<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="85px">
      <el-form-item label="报修人姓名" prop="username">
        <el-input
          v-model="queryParams.username"
          placeholder="请输入报修人姓名"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="工号" prop="jobId">
        <el-input
          v-model="queryParams.jobId"
          placeholder="请输入工号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="联系电话" prop="phone">
        <el-input
          v-model="queryParams.phone"
          placeholder="请输入联系电话"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <br>
      <el-form-item label="维修员姓名" prop="serviceName">
        <el-input
          v-model="queryParams.serviceName"
          placeholder="请输入维修员姓名"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="报修地址" prop="location">
        <el-input
          v-model="queryParams.location"
          placeholder="请输入报修地址"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="紧急程度" prop="emergency">
        <el-select v-model="queryParams.emergency" placeholder="请选择紧急程度" clearable size="small">
          <el-option
            v-for="dict in emergencyOptions"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <br>
      <el-form-item label="处理状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择处理状态" clearable size="small">
          <el-option
            v-for="dict in statusOptions"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="创建时间">
        <el-date-picker v-model="dateRange"
                        size="small"
                        style="width: 240px"
                        value-format="yyyy-MM-dd"
                        type="daterange"
                        range-separator="-"
                        start-placeholder="开始日期"
                        end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <div class="top-right-btn">
        <el-tooltip class="item" effect="dark" content="刷新" placement="top">
          <el-button size="mini" circle icon="el-icon-refresh" @click="handleQuery"/>
        </el-tooltip>
        <el-tooltip class="item" effect="dark" :content="showSearch ? '隐藏搜索' : '显示搜索'" placement="top">
          <el-button size="mini" circle icon="el-icon-search" @click="showSearch=!showSearch"/>
        </el-tooltip>
      </div>
    </el-row>

    <el-table v-loading="loading" :data="repairList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="序号" type="index" align="center" show-overflow-tooltip width="50px">
      </el-table-column>
      <!--      <el-table-column label="id" align="center" prop="id"/>-->
      <el-table-column label="姓名" align="center" prop="username"/>
      <el-table-column label="工号" align="center" prop="jobId"/>
      <el-table-column label="联系电话" align="center" prop="phone"/>
      <el-table-column label="报修地址" align="center" prop="location"/>
      <el-table-column label="问题描述" align="center" prop="problemInfo"/>
      <el-table-column label="紧急程度" align="center" prop="emergency" :formatter="emergencyFormat"/>
      <!--<el-table-column label="受理时间" align="center" prop="assignTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.assignTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="受理时长" align="center" prop="assignDuration"/>
      <el-table-column label="解决时间" align="center" prop="resolveTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.resolveTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="解决时长" align="center" prop="resolveDuration"/>-->
      <el-table-column label="处理状态" align="center" prop="status" :formatter="statusFormat"/>
      <el-table-column label="创建人" align="center" prop="createBy"/>
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view "
            @click="handleDetail(scope.row)"
          >详情
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.current"
      :limit.sync="queryParams.size"
      @pagination="getList"
    />

    <!-- 报修单详情对话框 -->
    <el-dialog :title="title" :visible.sync="openDetail" width="680px">
      <el-form ref="form" :model="form" label-width="85px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="姓名" prop="username">
              <el-input v-model="form.username" readonly="readonly"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="工号" prop="jobId">
              <el-input v-model="form.jobId" readonly="readonly"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="联系电话" prop="phone">
              <el-input v-model="form.phone" readonly="readonly"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="紧急程度" prop="emergency">
              <el-select disabled v-model="form.emergency" placeholder="请选择紧急程度">
                <el-option
                  v-for="dict in emergencyOptions"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="创建时间" prop="createTime">
              <el-date-picker readonly="readonly"
                              clearable size="small" style="width: 240px"
                              v-model="form.createTime"
                              type="string"
                              value-format="yyyy-MM-dd hh:mm:ss">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="状态">
              <el-select disabled v-model="form.status">
                <el-option
                  v-for="dict in statusOptions"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12" v-show="form.serviceName !== null">
            <el-form-item label="维修员" prop="serviceName">
              <el-input v-model="form.serviceName" readonly="readonly"/>
            </el-form-item>
          </el-col>
          <el-col :span="12" v-show="form.servicePhone !== null">
            <el-form-item label="维修电话" prop="servicePhone">
              <el-input v-model="form.servicePhone" readonly="readonly"/>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="报修地址" prop="location">
              <el-input v-model="form.location" readonly="readonly"/>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="问题描述" prop="problemInfo">
              <el-input v-model="form.problemInfo" type="textarea" readonly="readonly"/>
            </el-form-item>
          </el-col>
          <div v-if="form.assignTime !== null">
            <el-col :span="12">
              <el-form-item label="受理时间" prop="assignTime">
                <el-date-picker clearable size="small" style="width: 240px"
                                readonly="readonly"
                                v-model="form.assignTime"
                                type="String"
                                value-format="yyyy-MM-dd"
                >
                </el-date-picker>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="受理时长" prop="assignDuration">
                <el-input v-model="form.assignDuration"/>
              </el-form-item>
            </el-col>
          </div>
          <div v-if="form.resolveTime !== null">
            <el-col :span="12">
              <el-form-item label="解决时间" prop="resolveTime">
                <el-date-picker clearable size="small" style="width: 240px"
                                readonly="readonly"
                                v-model="form.resolveTime"
                                type="String"
                                value-format="yyyy-MM-dd"
                >
                </el-date-picker>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="解决时长" prop="resolveDuration">
                <el-input v-model="form.resolveDuration"/>
              </el-form-item>
            </el-col>
          </div>
          <el-col :span="24">
            <el-form-item label="解决回复" prop="reply" v-if="form.reply !== null">
              <el-input v-model="form.reply" type="textarea" readonly="readonly"/>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="评价" prop="comment" v-if="form.comment !== null">
              <el-input v-model="form.comment" type="textarea" readonly="readonly"/>
            </el-form-item>
          </el-col>
        </el-row>
        <div>
          <el-button
            type="primary" plain
            @click="handleResolve()"
            v-if="form.status === 'PROCESSING'"
            v-hasPerm="['repair_resolved']"
          >已 解 决
          </el-button>
        </div>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">关 闭</el-button>
      </div>
    </el-dialog>

    <!-- 解决回复操作 -->
    <el-dialog :title="reply" :visible.sync="openReply">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="24">
            <el-form-item label="解决回复" prop="replyCommit">
              <el-input v-model="form.replyCommit" type="textarea" placeholder="请输入解决回复"/>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitReply">确 定</el-button>
        <el-button @click="cancelReply">关 闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listRepair,
  getRepair,
  delRepair,
  addRepair,
  editRepair,
  exportRepair,
  repairAssign,
  repairComment,
  repairResolved, listMyService
} from "@/api/it/repair";
import {listServiceUser} from "@/api/system/user";

export default {
  name: "Repair",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 报修单表格数据
      repairList: [],
      // 弹出层标题(第一层)
      title: "",
      // 弹出层标题(解决回复)
      reply: "",
      // 是否显示弹出层(新增\修改)
      open: false,
      // 是否显示弹出层(详情)
      openDetail: false,
      // 是否显示弹出层(解决回复)
      openReply: false,
      // 日期范围
      dateRange: [],
      // 维修员列表
      serviceUserOptions: [],
      // 紧急程度字典
      emergencyOptions: [],
      // 处理状态字典
      statusOptions: [],                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        // 查询参数
      queryParams: {
        current: 1,
        size: 10,
        username: undefined,
        jobId: undefined,
        phone: undefined,
        serviceName: undefined,
        location: undefined,
        problemInfo: undefined,
        emergency: undefined,
        assignTime: undefined,
        resolveTime: undefined,
        status: undefined,
        photo: undefined,
        createTime: undefined,
      },
      // 显示搜索条件
      showSearch: true,
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        id: [
          {required: true, message: "id不能为空", trigger: "blur"}
        ],
        username: [
          {required: true, message: "报修人姓名不能为空", trigger: "blur"}
        ],
        jobId: [
          {required: true, message: "工号不能为空", trigger: "blur"}
        ],
        phone: [
          {required: true, message: "联系电话不能为空", trigger: "blur"},
          {
            pattern: /^1[3|4|5|6|7|8|9][0-9]\d{8}$/,
            message: "请输入正确的手机号码",
            trigger: "blur"
          }
        ],
        location: [
          {required: true, message: "报修地址不能为空", trigger: "blur"}
        ],
        problemInfo: [
          {required: true, message: "问题描述不能为空", trigger: "blur"}
        ],
        emergency: [
          {required: true, message: "紧急程度不能为空", trigger: "blur"}
        ],
        reply: [
          {required: true, message: "解决回复不能为空", trigger: "blur"}
        ],
      }
    };
  },
  created() {
    this.getList();
    this.getDicts("repair_emergency").then(response => {
      this.emergencyOptions = response.data;
    });
    this.getDicts("repair_status").then(response => {
      this.statusOptions = response.data;
    });
  },
  methods: {
    filterData() {
      var str = this.$refs.searchSelect.$data.selectedLabel;// 此属性得到输入的文字
      // 控制的js
      if (str.length > 20) {
        this.$refs.searchSelect.$data.selectedLabel = str.substr(0, 21)
      }
    },
    /** 查询报修单列表 */
    getList() {
      this.loading = true;
      listMyService(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
        this.repairList = response.data;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 紧急程度字典翻译
    emergencyFormat(row, column) {
      return this.selectDictLabel(this.emergencyOptions, row.emergency);
    },
    // 处理状态字典翻译
    statusFormat(row, column) {
      return this.selectDictLabel(this.statusOptions, row.status);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.openDetail = false;
      this.reset();
    },
    // 关闭回复操作界面按钮
    cancelReply() {
      this.openReply = false;
      this.form.replyCommit = undefined;
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        username: undefined,
        jobId: undefined,
        phone: undefined,
        serviceId: undefined,
        serviceName: undefined,
        servicePhone: undefined,
        location: undefined,
        problemInfo: undefined,
        comment: undefined,
        reply: undefined,
        replyCommit: undefined,
        emergency: undefined,
        assignTime: undefined,
        assignDuration: undefined,
        resolveTime: undefined,
        resolveDuration: undefined,
        status: undefined,
        photo: undefined,
        createBy: undefined,
        createTime: undefined,
        updateBy: undefined,
        updateTime: undefined,
        remarks: undefined,
        delFlag: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.current = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 解决回复按钮操作 */
    handleResolve() {
      this.openReply = true;
      this.reply = "解决回复";
    },
    /** 详情按钮操作 */
    handleDetail(row) {
      this.reset();
      const id = row.id
      getRepair(id).then(response => {
        this.form = response.data;
        this.openDetail = true;
        this.title = "报修单信息详情";
      });
    },
    /** 解决回复提交操作 */
    submitReply() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.reply = this.form.replyCommit;
          repairResolved(this.form).then(response => {
            if (response.code === 0) {
              this.msgSuccess("回复成功");
              this.openReply = false;
              this.openDetail = false;
              this.getList();
            } else {
              this.msgError(response.msg);
            }
          })
        }
      })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有报修单数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function () {
        return exportRepair(queryParams);
      }).then(response => {
        this.download(response.data);
      }).catch(function () {
      });
    }
  }
};
</script>
